﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using ExtAspNet;
using System.IO;
using FJASPX.Framwork.UI;
using FJASPX.Framwork.Constant;
using FJASPX.Framwork.Model;
using FJASPX.Framwork.Helper;

namespace FJASPX.Framwork.UI
{
    /// <summary>
    /// 管理页面基类
    /// </summary> 
    public abstract class EditPageBase<T> : PageBase where T : Entity
    {
        #region Properties属性

        /// <summary>
        /// 添加按钮
        /// </summary>
        protected Button btn_BReturn;

        /// <summary>
        /// 是更新还是添加（默认否）
        /// </summary>
        protected bool IsUpdate
        {
            get
            {
                return Convert.ToBoolean(this.ViewState[Strings.EDIT]);
            }
            set
            {
                this.ViewState[Strings.EDIT] = value;
            }
        }

        /// <summary>
        /// 是否返回
        /// </summary>
        protected bool EnableReturn
        {
            get
            {
                return Convert.ToBoolean(this.ViewState[Strings.IS_RETURN]);
            }
            set
            {
                this.btn_BReturn.Hidden = !value;
                this.ViewState[Strings.IS_RETURN] = value;
            }
        }

        /// <summary>
        /// 页面实体对象
        /// </summary>
        public T Model
        {
            get;
            set;
        } 
        #endregion

        #region Methods方法

        protected override bool FirstCallRead()
        {
            //是否返回
            if (!StringHelper.GetQueryString(Strings.IS_RETURN).IsEmpty())
            {
                this.btn_BReturn.Hidden = false;
                this.EnableReturn = true;
            }
            //是更新还是添加
            if (!StringHelper.GetQueryString(Strings.IS_UPDATE).IsEmpty())
            {
                this.IsUpdate = true;
            }
            return true;
        }

        /// <summary>
        /// 更新成功
        /// </summary>
        protected virtual void Success()
        {
            switch (IsUpdate)
            {
                case true:
                    if (this.EnableReturn)
                    {
                        Alert.ShowInTop(Message.UPDATE_SUCCESS, Message.SELECT_PROMPT, ActiveWindow.GetHidePostBackReference());
                        return;
                    }
                    Alert.ShowInTop(Message.UPDATE_SUCCESS);
                    break;
                case false:
                    Alert.ShowInTop(Message.ADD_SUCCESS, Message.SELECT_PROMPT, Strings.SCRIPT_RELOAD);
                    break;
            }
        }

        /// <summary>
        /// 更新失败
        /// </summary>
        protected virtual void Fail()
        {
            switch (IsUpdate)
            {
                case true:
                    Alert.ShowInTop(Message.UPDATE_FAIL);
                    break;
                case false:
                    Alert.ShowInTop(Message.ADD_FAIL);
                    break;
            }
        }

        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="yFile">原文件</param>
        /// <param name="mFile">目标文件</param>
        /// <param name="path">目标目录</param>
        /// <returns></returns>
        protected virtual string SavePic(string yFile, string mFile, string path)
        {
            string cFilePath = Server.MapPath(Cfg.CACHE_PAGE_PATH + mFile);
            string filePath = Server.MapPath(path + mFile);
            if (File.Exists(cFilePath))
            {
                File.Copy(cFilePath, filePath);
                File.Delete(cFilePath);
                StringHelper.MakeThumbnail(filePath, filePath.GetMin(), 0, 200, 1);
                yFile = Server.MapPath(yFile);
                if (File.Exists(yFile))
                {
                    File.Delete(yFile);
                    File.Delete(yFile.GetMin());
                }
            }
            return path + mFile;
        }

        #endregion

        #region Events事件

        /// <summary>
        /// 返回
        /// </summary>
        protected void btn_Return_Click(object sender, EventArgs e)
        {
            if (this.EnableReturn)
            {
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
        }

        #endregion
    }
}
